package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/application")
@Tag(name = "申请模块")
@Slf4j
public class ApplicationController {
    @Autowired
    private ApplicationService applicationService;

    @PostMapping("save")
    @Operation(summary = "发起申请单")
    @ApiOperationSupport(order = 10)
    public JsonResult addApplication(@Validated ApplicationSaveParam applicationSaveParam) {
        log.debug("开始处理【添加申请】的业务，参数：{}", applicationSaveParam);
        applicationService.addApplication(applicationSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    @Operation(summary = "查询申请单")
    @ApiOperationSupport(order = 20)
    public JsonResult selectApplication(ApplicationQuery applicationQuery) {
        log.debug("开始处理【查询申请单】的业务");
        List<ApplicationVO> list = applicationService.selectApplication(applicationQuery);
        return JsonResult.ok(list);
    }

    @GetMapping("cancel/{id}")
    @Operation(summary = "撤销申请单")
    @ApiOperationSupport(order = 30)
    public JsonResult cancelApplication(@PathVariable Long id) {
        log.debug("开始处理【撤销申请单】的业务");
        applicationService.cancelApplication(id);
        return JsonResult.ok();
    }

    @PostMapping("distribute/{applicationId}/{vehicleId}")
    @Operation(summary = "分配车辆")
    @ApiOperationSupport(order = 40)
    public JsonResult updateApplication(@PathVariable Long applicationId, @PathVariable Long vehicleId){
        log.debug("开始处理【分配车辆】的业务，参数：applicationId:{},vehicleId:{}", applicationId, vehicleId);
        applicationService.distributeApplication(applicationId, vehicleId);
        return JsonResult.ok();
    }


    @PostMapping("back/{applicationId}/{vehicleId}")
    @Operation(summary = "归还车辆")
    @ApiOperationSupport(order = 50)
    public JsonResult backApplication(@PathVariable Long applicationId, @PathVariable Long vehicleId){
        log.debug("开始处理【归还车辆】的业务，参数：applicationId:{},vehicleId:{}", applicationId, vehicleId);
        applicationService.backApplication(applicationId, vehicleId);
        return JsonResult.ok();
    }
}
